package com.nervenets.web.redis;

import com.nervenets.web.redis.domain.GlobalRule;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by Joe on 2018/1/6.
 */
@Service
public class GlobalVariablesService {
    private static final int _OPERATION_INTERVAL_SECONDS_ = 3;
    private static final String _OPERATION_INTERVAL_KEY_ = "_LC_INTERVAL_%s_";
    private static final String _OPERATION_CUSTOM_INTERVAL_KEY_ = "_LC_OPERATION_CUSTOM_INTERVAL_KEY_%s_";
    private static final String _WE_CHAT_AUTH_TOKEN_ = "_LC_WE_CHAT_AUTH_TOKEN_";
    private static final String _SESSION_ = "_LC_SESSION_%s_%s_";
    @Resource
    private RedisDao redisDao;
    @Resource
    private RedisSetDao redisSetDao;
    @Resource
    private RedisOrderedSetDao redisOrderedSetDao;


    /**
     * 用户能否在操作时间间隔内继续操作
     *
     * @param userId 用户ID
     * @return 能否继续操作
     */
    public boolean canProceed(long userId) {
        String proKey = String.format(_OPERATION_INTERVAL_KEY_, userId);
        if (redisDao.setNx(proKey, proKey)) {
            redisDao.setExpired(proKey, _OPERATION_INTERVAL_SECONDS_);
            return true;
        } else {
            return false;
        }
    }

    public boolean canProceedCustom(String key, int expire) {
        String proKey = String.format(_OPERATION_CUSTOM_INTERVAL_KEY_, key);
        if (redisDao.setNx(proKey, proKey)) {
            redisDao.setExpired(proKey, expire);
            return true;
        } else {
            return false;
        }
    }

    public void setSession(long userId, String key, String value) {
        setSession(userId, key, value, 600);
    }

    public void setSession(long userId, String key, String value, int expired) {
        String redisKey = String.format(_SESSION_, userId, key);
        redisDao.addString(redisKey, value);
        if (expired > 0) {
            redisDao.setExpired(redisKey, expired);
        }
    }

    public String getSession(long userId, String key) {
        String redisKey = String.format(_SESSION_, userId, key);
        if (redisDao.exist(redisKey)) {
            return redisDao.getString(redisKey);
        }
        return null;
    }

    public Long getSessionExpired(long userId, String key) {
        String redisKey = String.format(_SESSION_, userId, key);
        if (redisDao.exist(redisKey)) {
            return redisDao.getExpired(redisKey);
        }
        return null;
    }

    public GlobalRule getGlobalRule() {
        GlobalRule rule = new GlobalRule();
        if (redisDao.exist(rule.getRedisKey())) {
            rule = redisDao.getRedisDomain(rule.getRedisKey(), GlobalRule.class);
        }
        return rule;
    }

    public void setGlobalRule(GlobalRule globalRule) {
        redisDao.saveOrUpdate(globalRule);
    }

    public long incrBy(String key, long offset, int expired) {
        long result = redisDao.incrBy(key, offset);
        if (expired > 0) redisDao.setExpired(key, expired);
        return result;
    }
}
